package com.qqtech.qquan.hi.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.agent.context.AdminPage;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.hi.enums.HiRuleClassifyDictEnum;
import com.qqtech.qquan.hi.model.HiRuleResult;
import com.qqtech.qquan.hi.model.HiRuleResultQuery;
import com.qqtech.qquan.hi.model.HiRuleResultVo;
import com.qqtech.qquan.hi.model.HiRuleVo;
import com.qqtech.qquan.hi.service.HiRuleResultService;
import com.qqtech.qquan.hi.service.HiRuleService;
import com.qqtech.qquan.sys.controller.BaseAgentController;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;

@Controller
@RequestMapping(value = "/hi/rule/result")
public class HiRuleResultController extends BaseAgentController {

	@Resource
	private HiRuleResultService hiRuleResultService;
	
	@Resource
	private UserBaseService userBaseService;
	
	@Resource
	private HiRuleService hiRuleService;
	
	@Override
	protected BaseService<HiRuleResult> getBaseService() {
		return hiRuleResultService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/hi/hiRuleResult-list";
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(HiRuleResultQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<HiRuleResultVo> page = hiRuleResultService.queryPageList4Admin(query, pageKit);
		HiRuleVo ruleVo = null;
		for (HiRuleResultVo ruleResult : page) {
			ruleResult.setStrClassify(HiRuleClassifyDictEnum.getDesc(ruleResult.getClassify()));
			ruleVo = hiRuleService.getById(ruleResult.getRuleId());
			if(ruleVo!=null){
				ruleResult.setRuleName(ruleVo.getName());
			}
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	/**
	 * 去更新
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/update")
	public String update(Integer id, Model model) {
		HiRuleResultVo hiRuleResultVo = hiRuleResultService.getById(id);
		model.addAttribute("hiRuleResultVo", hiRuleResultVo);
		return "/hi/hiRuleResult-update";
	}
	
	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/doUpdate")
	@ResponseBody
	public Result doUpdate(HiRuleResult hiRuleResult) {
		Result ret = new Result();
		hiRuleResultService.updateById(hiRuleResult);
		return ret;
	}
	
	@RequestMapping(value = "/detail")
	public String detail(HiRuleResultQuery query, Model model) {
		HiRuleResultVo hiRuleResultVo = hiRuleResultService.getById(query.getId());
		
		if (hiRuleResultVo != null) {
			UserBaseVo userBaseVo=userBaseService.getById(hiRuleResultVo.getUserId());
			if(userBaseVo!=null){
				hiRuleResultVo.setNick(userBaseVo.getNick());
			}			
			HiRuleVo hiRuleVo = hiRuleService.getById(hiRuleResultVo.getRuleId());
			if(hiRuleVo!=null){
				hiRuleResultVo.setRuleName(hiRuleVo.getName());
			}
			hiRuleResultVo.setStrClassify(HiRuleClassifyDictEnum.getDesc(hiRuleResultVo.getClassify()));
			model.addAttribute("ruleResult", hiRuleResultVo);
		}
		return "/hi/hiRuleResult-detail";
	}
}
